<?php
/*
 * @Author: your name
 * @Date: 2019-11-18 13:23:58
 * @LastEditTime : 2020-01-07 19:07:45
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /ahsmp_3/application/common/service/HostLink.php
 */

namespace app\common\service;


use think\Db;

class HostLink extends BaseService
{
    /**
     * @name: 查找单个模块的联系地址
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-09 13:52:24
     */
    public function findOne ($hostId, $module)
    {
        $return = Db::name('HostLink')->field('ip,port')->where('host_id', $hostId)->where('module', $module)->find();

        return $return;
    }

    /**
     * @name: 模块是否在线
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-26 16:42:28
     */
    public function checkOnline ($module)
    {
        $hostId_a = $this->getHost('id');
        $offlineHost = Db::name('HostLink')->whereIn('host_id', $hostId_a)->where('module', $module)->where('status', 0)->column('host_id');

        if($offlineHost)
        {
            $this->setError($this->CODE_FAIL, $this->hostErrorMessage($offlineHost, '已离线'));
            return false;
        }
        return true;
    }

    /**
     * @name: 添加
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-30 14:04:36
     */
    public function add ($module, $hostId_a)
    {
        if(empty($hostId_a))
        {
            return [];
        }

        $insertData = array_map(function($host_id) use ($module)
        {
            $data = [
                'host_id' => $host_id,
                'module' => $module,
                'ip' => 0,
                'port' => 0,
                'status' => 0,
            ];
            return "('" . implode("','", $data) . "')";
        }, $hostId_a);

        return Db::execute('INSERT IGNORE INTO `' . config('database.prefix') .'host_link` (`host_id`, `module`, `ip`, `port`, `status`) VALUES ' . implode(',', $insertData));
    }

    /**
     * @name: 主机离线
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2020-01-07 17:59:31
     */
    public function offline ($hostId_a, $module)
    {
        Db::name('HostLink')->whereIn('host_id', $hostId_a)->where('module', $module)->update(['status' => 0]);
    }

    /**
     * @name: 修改版本
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2020-01-07 19:06:17
     */
    public function updateVersion ($host_id, $module, $version)
    {
        Db::name('HostLink')->where('host_id', $host_id)->where('module', $module)->update(['version' => $version]);
    }

    /**
     * @name 查询所有客户端的id
     * @return array
     * @author wx
     * @date 2020/5/7 13:41
     */
    public function findAllHostId()
    {
        $hostIdArr = Db::name('host_link')->where('module','client')->column('host_id');
        return $hostIdArr;
    }

    /**
     * @name 修改
     * @param $hostId
     * @author wx
     * @date 2020/5/7 16:40
     */
    public function modify($hostId, $data)
    {

        $res = Db::name('host_link')->where('host_id',$hostId)->where('module','client')->update($data);

        return $res;
    }

    /**
     * @name 根据host_id获取netfilter的连接
     * @param $hostId
     * @return array|\PDOStatement|string|\think\Collection
     * @author wx
     * @date 2020/11/17 14:13
     */
    public function findNetfilterdata($hostId)
    {
        $return = Db::name('host_link')->field('ip, port')
            ->where('module','netfilter')
            ->where('host_id', $hostId)
            ->select();

        return $return;
    }

    /**
     * @name 判断主机是否在线
     * @author wx
     * @date 2020/12/22 15:08
     */
    public function checkOnlineByHostId($hostId, $module)
    {
        $count = Db::name('host_link')
            ->where('host_id', $hostId)
            ->where('module', $module)
            ->where('status', 1)
            ->count();

        if($count == 0){
            $online = false;
        }else{
            $online = true;
        }

        return $online;
    }
}